/**
 * @Author: LiMeng
 * @Date: 2017/6/9
 */
var GLOBAL = {
    ID: $.getQueryParam("id"),
    ACTION_URL: ROOT_PATH + '/workLicense/limitLicense/add.shtml'
};
var ajaxSuccessHandler = function (result) {
    result = $.parseJSON(result);
    if (result.success) {
        $.messager.progress('close');	// 如果提交成功则隐藏进度条
        layer.msg(result.message, {time: 1000, icon: 1}, function () {
            WindowManager.closeLayer({returnValue: true});
        });
    } else {
        $.messager.progress('close');	// 如果提交成功则隐藏进度条
        layer.msg(result.message, {icon: 2});
    }
};
var addCommonFormParams = function (param) {
    param.applyDeptName = $('#applyDeptId').combobox('getText');
    param.dangerTypeNames = $('#dangerTypeIds').combobox('getText');
};
var formSubmitAction = function (url, params) {
    $.messager.progress();	// 显示进度条

    $('#form1').form('submit', {
        url: url,
        onSubmit: function (param) {
            addCommonFormParams(param);
            if ($.isNotEmpty(params)) {
                $.extend(param, params);
            }
            var isValid = $(this).form('validate');
            if (!isValid) {
                $.messager.progress('close');	// 如果表单是无效的则隐藏进度条
            }
            return isValid;	// 返回false终止表单提交
        },
        success: function (result) {
            ajaxSuccessHandler(result);
        }
    });
};
var getSelectedContractorAudit = function (data) {
    data = data[0];
    $('#contractorName').textbox('setValue', data.contractorName);
    $('#contractorAuditId').val(data.id);
    $('#contractorId').val(data.contractorId);
};
var getSelectedUser = function (data, target) {
    data = data[0];

    $('#' + target + 'Name').textbox('setValue', data.realName);
    $('#' + target + 'Id').val(data.id);
};
var openSelectUser = function (title, target) {
    WindowManager.openLayer({
        type: 2,
        title: title,
        area: ['900px', '500px'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml',
        callBack: function (data) {
            getSelectedUser(data, target);
        }
    });
};
var getSelectedContractorStaff = function (data, target) {
    data = data[0];

    $('#' + target + 'Name').textbox('setValue', data.name);
    $('#' + target + 'Id').val(data.id);
};
var openSelectContractorStaff = function (title, target) {
    var contractorId = $('#contractorId').val();
    if (contractorId === '' ) {
        layer.alert('请先选择施工单位！');
        return;
    }
    WindowManager.openLayer({
        type: 2,
        title: title,
        area: ['700px', '400px'],
        content: ROOT_PATH + '/contractor/contractorStaff/simpleList.shtml?contractorId=' + contractorId,
        callBack: function (data) {
            getSelectedContractorStaff(data, target);
        }
    });
};
$(function () {

    $('#dangerTypeIds').combobox({
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml?code=' + CONSTANT.DictionaryTypeDefConstants.DANGER_TYPE,
        valueField: 'id',
        editable: false,
        multiple: true,
        required: true
    });

    $('#applyUserName').textbox({
        required: true,
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectUser('选择申请人', 'applyUser')
            }
        }],
        editable: false
    });

    $('#produceUserName').textbox({
        required: true,
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectUser('选择生产单位安全负责人', 'produceUser')
            }
        }],
        editable: false
    });

    $('#produceStaffName').textbox({
        required: true,
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectContractorStaff('选择施工单位安全负责人', 'produceStaff')
            }
        }],
        editable: false
    });

    $('#samplingUserName').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectUser('选择分析人', 'samplingUser')
            }
        }],
        editable: false
    });

    $('#applyDeptId').combotree({
        url: ROOT_PATH + '/permission/department/getDepartmentTree.shtml?parentId=0',
        editable: false
    });

    $('#contractorName').textbox({
        required: true,
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                WindowManager.openLayer({
                    type: 2,
                    title: '选择承包商',
                    area: ['700px', '400px'],
                    content: ROOT_PATH + '/contractor/contractorAudit/auditedList.shtml',
                    callBack: getSelectedContractorAudit
                });
            }
        }],
        editable: false
    });

    $('#btn_save').on('click', function () {
        formSubmitAction(GLOBAL.ACTION_URL);
    });
});